<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    
    public function __construct(){
        parent::__construct();
          
        $this->model = $this->getModel();
        $this->aut = new bgAuthorize();
    }
    
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    /**
     * Show list question when click next button
    **/
    public function showQuestion(){
        $serq_code = JRequest::getCmd('serq_code');
        $list_page = JRequest::getCmd('list_page');
        $index = JRequest::getCmd('index');
        $user_id = $this->aut->getUserID();
        //print_r($serq_code);die;

        /* REMOVE RESQ neu ban cau hoi do user da tra loi roi */
        $this->model->removeresq($serq_code,$user_id);
        /*  REMOVE SESS neu ban cau hoi do user da tra loi roi */
        $this->model->removesess($serq_code,$user_id);
        
        /* list page */
        if($list_page != null){
            $list_page = explode("_",$list_page);
        }
        /* get page current */
        $page = $list_page[$index];
        
        /* get list question and list answer */
        $list_question = $this->model->getListQuestion($serq_code,$page);
        $list_ansq = $this->model->getListAnsq($serq_code);
        
        /* check list answer of question */
        if($list_ansq == null){
            /* save Session in database */
            $data['SESS_CODE'] = HelperBG::getIDAuto('sess','SESS_CODE',20);
            $data['SESS_CODE'] = $data['SESS_CODE']*1;
            $data['SESS_USER_CODE'] = $user_id;
            $data['SESS_PLAY_TIME'] = date('Y-m-d H:i:s');
            $data['SESS_SERQ_CODE'] = $serq_code;
            $data['SESS_IP'] = null;
            $data['SESS_COMPLETED'] = 0;
            $data['SESS_PUBLISHED'] = 0;

            

            $this->model->saveSessInsert($data);
            
            echo '<p align="center">データは見つかりませんでした</p>';
        }else{
            /* save Session in database */
            $data['SESS_CODE'] = HelperBG::getIDAuto('sess','SESS_CODE',20);
            $data['SESS_CODE'] = $data['SESS_CODE']*1;
            $data['SESS_USER_CODE'] = $user_id;
            $data['SESS_PLAY_TIME'] = date('Y-m-d H:i:s');
            $data['SESS_SERQ_CODE'] = $serq_code;
            $data['SESS_IP'] = null;
            $data['SESS_COMPLETED'] = 0;
            $data['SESS_PUBLISHED'] = 0;
            //echo "<pre>";print_r($data);echo "</pre>";
            $this->model->saveSessInsert($data);
            
            /* show question */
            if($list_question != null){
            
            echo '<div class="intro_page">'.$list_question[$index]->PAGQ_TITLE.'</div>
                  <div class="content_intro">'.$list_question[$index]->PAGQ_DESCRIPTION.'</div>';
                  
                for($i = 0; $i < count($list_question); $i++){
                    $value = $list_question[$i];
                    
            echo '<table style="width: 100%;">
                    <tbody>
                    <tr>
                        <td>
                        <div class="question">
                        
                            <div class="ques">
                                <label>'. ($i+1) .'.'. $value->QUES_TITLE .'</label>';
            if($value->QUES_REQUIRED == 1){ echo '<label class="required">※</label>'; }
            if(!empty($value->QUES_DESCRIPTION)){ echo '<br /><label>('.$value->QUES_DESCRIPTION.')</label>'; }
            echo            '</div>
                            <div class="answ">';
                            
            if($value->QUES_TYPE == 1){
                if($list_ansq != null){
                    
                    
                    if($value->QUES_REQUIRED == 1){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';  
                    }
                    
                    for($j = 0; $j < count($list_ansq); $j++){
                        if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                            $val = $list_ansq[$j];
                            echo '<input type="checkbox" class="checkbox_ans"';
                            if($value->QUES_REQUIRED == 1){
                                echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                            }
                            echo ' name="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" id="cb'.$value->QUES_CODE."_".$val->ANSQ_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_1".'" /><label for="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                    }
                }                                
            }
                            
                            
            if($value->QUES_TYPE == 2){
                if($list_ansq != null){
                    
                    if($value->QUES_REQUIRED == 1){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';  
                    }
                    
                    for($j = 0; $j < count($list_ansq); $j++){
                        if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                            $val = $list_ansq[$j];
                            
                            echo '<input type="radio" class="radio_ans"';
                            if($value->QUES_REQUIRED == 1){
                                echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                            }
                            echo  ' id="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" name="rd'.$value->QUES_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_2".'"/><label for="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                    }
                }                                
            }
            
            
                            
                            
            if($value->QUES_TYPE == 3){
                echo '<textarea name="'.$value->QUES_CODE.'" id="'.$value->QUES_CODE.'" class="text_ans" cols="8" rows="3"';
                if($value->QUES_REQUIRED == 1){
                    echo ' check = "1" ';
                }else{
                    echo ' check = "0" ';
                }
                echo ' style="width:300px" ></textarea>';                                
            }
                            
                                
            echo           '</div>
                        </div>
                    </td>
                </tr>
                </tbody>
                </table>';
            
                }
            
            echo '<table style="width: 100%;">
                    <tfoot>
            			<tr>
            				<td >
            					<div class="container" style="margin-left:300px">
                                    <div class="prev_page">';
                                    if($index == 0){
           echo                         '<div class="button2-right off"><div class="prev"><span>前へ</span></div></div>';
                                    }else{
           echo                         '<div class="button2-right">
                                            <div class="prev"><a href="#" title="Prev" class="link_page" id="link_page_prev" onclick="showQuesPrev('.$serq_code.",".($index-1).",".$data['SESS_CODE'].');" >前へ</a></div>
                                        </div>';
                                    }
            
           echo                     '</div>
                                    <div class="next_page">';
                                    if($index == (count($list_page)-1)){
           echo                         '<div class="button2-left"><div class="end"><a href="#" title="End" class="link_end" id="link_end" onclick="showQuesEnd('.$serq_code.",".$data['SESS_CODE'].');" >終わり</a></div></div>';
                                    }else{
           echo                         '<div class="button2-left">
                                            <div class="next"><a href="#" title="Next" class="link_page" id="link_page_next" onclick="showQuesNext('.$serq_code.",".($index+1).",".$data['SESS_CODE'].');" >次へ</a></div>
                                        </div>';
                                    }   
           echo                     '</div>
                                </div>
                                </div>				
                            </td>
            			</tr>
            		</tfoot>
                </table>';
                
            }
        
        }        
        
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    /**
     * Show list question when click next button
     * Qui dinh: - Tao mot attribute co ten la check=0 hoac check=1 de kiem tra co la cau hoi bat buoc khong
     *           - Gia tri cua moi cau tra loi gom(ky tu e, QUES_CODE, ANSQ_CODE, QUES_TYPE), e la cho biet dang lam moi hay chinh sua tra loi
     *           - Ket qua tra loi se luu vao mot mang gom: SESS_CODE, QUES_CODE, ANSQ_CODE 
    **/
    public function showQuesNext(){
        $serq_code = JRequest::getCmd('serq_code');
        $list_page = JRequest::getCmd('list_page');
        $index = JRequest::getCmd('index');
        $sess_code = JRequest::getCmd('sess_code');
        $res = JRequest::getCmd('result');
        //chuyen ve chuoi da ma hoa
        $res = str_replace("-","%",$res);
         
                   
        /* list page */
        if($list_page != null){
            $list_page = explode("_",$list_page);
        }
        
        /*page current */
        $page = $list_page[$index];
        
        /* get list question and list answer */
        $list_question = $this->model->getListQuestion($serq_code,$page);
        $list_ansq = $this->model->getListAnsq($serq_code);
        
        /* get list result */
        $list = $this->model->getListResult($sess_code);
        $list_result = array();
        $list_resqcode = array();
        $list_require = array();
        $arr_quesedit = array();
        
        for($i = 0; $i < count($list); $i++){
            if($list[$i]->RESQ_TYPE == 1){
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_CODE;
                                 
            }else{
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_CODE;                
            }
            //tao mang cau hoi duoc tra loi de kiem tra ren buoc
            $list_require[$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_QUES_CODE;
            // mang cac ques_code duoc chinh sua
            $arr_quesedit[$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_QUES_CODE;
            
        }
        
        /* list answer of user */
        if($res != ''){
            $result = explode(".",$res);
        }
                        
        /* save answer of user */
        $data = array();
        $data_edit = array();
        $data_del = array();
        
        $key = HelperBG::getIDAuto('resq','RESQ_CODE',20);
        $key = $key*1;
        
        if($result != null){
            foreach($result as $value){
                $arr = array();
                
                $temp = explode("_",$value);
                if(($arr_quesedit != null) && ($arr_quesedit[$temp[0]] == $temp[0])){
                    if($temp[2] == 1){
                        //DATA DELETE
                        $arr_del = array();
                        $arr_del['SESS_CODE'] = $sess_code;
                        $arr_del['QUES_CODE'] = $temp[0];
                        $data_del[] = $arr_del;                        
                        
                        // DATA INSERT
                        $arr['RESQ_CODE'] = $key;
                        $key++;
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        $arr['RESQ_VALUE'] = $temp[1];
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data[] = $arr;
                    }else{
                        $arr['RESQ_CODE'] = $list_resqcode[$sess_code."_".$temp[0]];
                        
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        
                        //giai ma gia tri da ma hoa
                        $str = urldecode($temp[1]);
                        $arr['RESQ_VALUE'] = $str;
                        
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data_edit[] = $arr;                      
                    }
                                   
                    
                }else{
                    $arr['RESQ_CODE'] = $key;
                    $key++;
                    $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                    $arr['RESQ_QUES_CODE'] = $temp[0];
                    
                    //giai ma gia tri da ma hoa
                    $str = urldecode($temp[1]);
                    $arr['RESQ_VALUE'] = $str;
                    $arr['RESQ_TYPE'] = $temp[2];
                    
                    $data[] = $arr;
                }
                
            }
        }
        
        //echo "<pre>";print_r($data);echo "</pre>";
        //echo "<br/>";
        //echo "<pre>";print_r($data_edit);echo "</pre>";
        
        if($data_del != null){
            $this->model->removeResqFrSessQuess($data_del);
        }
        if($data != null){
            $this->model->saveResqInsert($data);
        }
        if($data_edit != null){
            $this->model->saveResqUpdate($data_edit);
        }
        
        
        /* show list question */
        if($list_question != null){
        
        echo '<div class="intro_page">'.$list_question[$index]->PAGQ_TITLE.'</div>
              <div class="content_intro">'.$list_question[$index]->PAGQ_DESCRIPTION.'</div>';
              
            for($i = 0; $i < count($list_question); $i++){
                $value = $list_question[$i];
        echo '<table style="width: 100%;">
                <tbody>
                <tr>
                    <td>
                    <div class="question">
                    
                        <div class="ques">
                            <label>'. ($i+1) .'.'. $value->QUES_TITLE .'</label>';
        if($value->QUES_REQUIRED == 1){ echo '<label class="required">※</label>'; }
        if(!empty($value->QUES_DESCRIPTION)){ echo '<br /><label>('.$value->QUES_DESCRIPTION.')</label>'; }
        echo            '</div>
                        <div class="answ">';
        
        /* hien cau tra loi cua cau hoi */           
        if($value->QUES_TYPE == 1){
            if($list_ansq != null){
                
                if($value->QUES_REQUIRED == 1){
                    if($list_require[$value->QUES_CODE] == $value->QUES_CODE){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="1" />';
                    }else{
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';
                    }                      
                }
                
                for($j = 0; $j < count($list_ansq); $j++){
                    if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                        $val = $list_ansq[$j];
                        echo '<input type="checkbox" class="checkbox_ans"';
                        
                        if($value->QUES_REQUIRED == 1){
                            echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                        }
                                                
                        if($list_result[$sess_code."_".$value->QUES_CODE."_".$list_ansq[$j]->ANSQ_CODE] == $list_ansq[$j]->ANSQ_CODE){
                            echo 'checked="checked"  name="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" id="cb'.$value->QUES_CODE."_".$val->ANSQ_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_1".'" /><label for="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                            
                        }else{
                            echo ' name="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" id="cb'.$value->QUES_CODE."_".$val->ANSQ_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_1".'" /><label for="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                        
                    }
                }
            }                                
        }
                        
                        
        if($value->QUES_TYPE == 2){
            if($list_ansq != null){
                
                if($value->QUES_REQUIRED == 1){
                    if($list_require[$value->QUES_CODE] == $value->QUES_CODE){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="1" />';
                    }else{
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';
                    }
                      
                }
                
                for($j = 0; $j < count($list_ansq); $j++){
                    if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                        $val = $list_ansq[$j];
                        echo '<input type="radio" class="radio_ans"';
                        
                        if($value->QUES_REQUIRED == 1){
                            echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                        }
                        
                        if($list_result[$sess_code."_".$value->QUES_CODE] == $list_ansq[$j]->ANSQ_CODE){
                            echo 'checked="checked" id="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" name="rd'.$value->QUES_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_2".'"/><label for="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                            
                        }else{
                            echo ' id="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" name="rd'.$value->QUES_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_2".'"/><label for="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                        
                    }
                }
            }                                
        }
        
        
                        
                        
        if($value->QUES_TYPE == 3){
            echo '<textarea name="'.$value->QUES_CODE.'" id="'.$value->QUES_CODE.'" class="text_ans"';
                    if($value->QUES_REQUIRED == 1){
                        echo ' check = "1" ';
                    }else{
                        echo ' check = "0" ';
                    }
                echo ' cols="8" rows="3" style="width:300px" >';
                echo $list_result[$sess_code."_".$value->QUES_CODE];
                echo '</textarea>';
                                                            
        }
                        
                            
        echo           '</div>
                    </div>
                </td>
            </tr>
            </tbody>
            </table>';
        
            }
        
        echo '<table style="width: 100%;">
                <tfoot>
        			<tr>
        				<td >
        					<div class="container" style="margin-left:300px">
                                <div class="prev_page">';
                                if($index == 0){
       echo                         '<div class="button2-right off"><div class="prev"><span>前へ</span></div></div>';
                                }else{
       echo                         '<div class="button2-right">
                                        <div class="prev"><a href="#" title="Prev" class="link_page" id="link_page_prev" onclick="showQuesPrev('.$serq_code.",".($index-1).",".$sess_code.');" >前へ</a></div>
                                    </div>';
                                }
        
       echo                     '</div>
                                <div class="next_page">';
                                if($index == (count($list_page)-1)){
       echo                         '<div class="button2-left"><div class="end"><a href="#" title="End" class="link_end" id="link_end" onclick="showQuesEnd('.$serq_code.",".$sess_code.');" >終わり</a></div></div>';
                                }else{
       echo                         '<div class="button2-left">
                                        <div class="next"><a href="#" title="Next" class="link_page" id="link_page_next" onclick="showQuesNext('.$serq_code.",".($index+1).",".$sess_code.');" >次へ</a></div>
                                    </div>';
                                }   
       echo                     '</div>
                            </div>
                            </div>				
                        </td>
        			</tr>
        		</tfoot>
            </table>';
            
        }        
        
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    /**
     * Show list question when click next button
    **/
    public function showQuesEnd(){
        $serq_code = JRequest::getCmd('serq_code');
        $sess_code = JRequest::getCmd('sess_code');
        $res = JRequest::getCmd('result');
        
        //chuyen chuoi ve dang ma hoa
        $res = str_replace("-","%",$res);
                
        /* update table SESSION */
        /* save Session in database */
        $sess['SESS_CODE'] = $sess_code;
        $sess['SESS_USER_CODE'] = $this->aut->getUserID();
        $sess['SESS_PLAY_TIME'] = date('Y-m-d H:i:s');
        $sess['SESS_SERQ_CODE'] = $serq_code;
        $sess['SESS_IP'] = null;
        $sess['SESS_COMPLETED'] = 1;
        $sess['SESS_PUBLISHED'] = 1;
        
        $this->model->saveSessUpdate($sess);
        
        /* get list result */
        $list = $this->model->getListResult($sess_code);
        $list_result = array();
        $list_resqcode = array();
        $arr_quesedit = array();
        
        for($i = 0; $i < count($list); $i++){
            if($list[$i]->RESQ_TYPE == 1){
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_CODE;
            }else{                
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_CODE;
            }
                        
            $arr_quesedit[$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_QUES_CODE;
        }
        
        /* list answer of user */
        if($res != ''){
            $result = explode(".",$res);
        }
              
        /* save answer of user */
        $data = array();
        $data_edit = array();
        $data_del = array();
        
        
        $key = HelperBG::getIDAuto('resq','RESQ_CODE',20);
        $key = $key*1;
        if($result != null){
            foreach($result as $value){
                $arr = array();
                
                $temp = explode("_",$value);
                if(($arr_quesedit != null) && ($arr_quesedit[$temp[0]] == $temp[0])){
                    if($temp[2] == 1){
                        //DATA DELETE
                        $arr_del = array();
                        $arr_del['SESS_CODE'] = $sess_code;
                        $arr_del['QUES_CODE'] = $temp[0];
                        $data_del[] = $arr_del;                        
                        
                        // DATA INSERT
                        $arr['RESQ_CODE'] = $key;
                        $key++;
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        $arr['RESQ_VALUE'] = $temp[1];
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data[] = $arr;
                    }else{
                        $arr['RESQ_CODE'] = $list_resqcode[$sess_code."_".$temp[0]];
                        
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        
                        //giai ma gia tri da ma hoa
                        $str = urldecode($temp[1]);
                        $arr['RESQ_VALUE'] = $str;
                        
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data_edit[] = $arr;                      
                    }
                                   
                    
                }else{
                    $arr['RESQ_CODE'] = $key;
                    $key++;
                    $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                    $arr['RESQ_QUES_CODE'] = $temp[0];
                    
                    //giai ma gia tri da ma hoa
                    $str = urldecode($temp[1]);
                    $arr['RESQ_VALUE'] = $str;
                        
                    $arr['RESQ_TYPE'] = $temp[2];
                    
                    $data[] = $arr;
                }
                
            }
        }
        //echo "<pre>";print_r($data);echo "</pre>";
        //echo "<br/>";
        //echo "<pre>";print_r($data_edit);echo "</pre>";
        
        if($data_del != null){
            $this->model->removeResqFrSessQuess($data_del);
        }
        if($data != null){
            $this->model->saveResqInsert($data);
        }
        if($data_edit != null){
            $this->model->saveResqUpdate($data_edit);
        }
        
        
        /* get info questionar */
        $row = $this->model->getQuestionarEnd($serq_code);
        
        /* show info questionar detail */
        
        echo '<div class="div_header">
                <p class="div_title">'.$row->SERQ_TITLE.'</p>             
             </div>
             <div class="cls"></div>
             <div class="div_endtitle">
                <p class="p_title" >'.$row->SERQ_END_TITLE.'</p>
                <p class="p_title" >'.$row->SERQ_END_DESCRIPTION.'</p>
             </div>
             <div class="cls"></div>
             <div class="div_img">';                   
                
        if(!empty($row->SERQ_IMG)){
            $path = "uploads/images/com_questionare/".$row->SERQ_IMG;
            if(file_exists($path)){ 
                echo '<td><img src="'.$path.'" style="width: 200px;" /></td>';
            }else{
                //echo '<img src="components/com_frquestion/public/images/exam.jpg" style="width: 200px;" />';
            }
        }else{
            //echo '<img src="components/com_frquestion/public/images/exam.jpg" style="width: 200px;" />';
        }
                               
        echo '</div>
             <div class="cls"></div>';
        
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    /**
     * Show list question when click prev button
    **/
    public function showQuesPrev(){
        $serq_code = JRequest::getCmd('serq_code');
        $list_page = JRequest::getCmd('list_page');
        $index = JRequest::getCmd('index');
        $sess_code = JRequest::getCmd('sess_code');
        $res = JRequest::getCmd('result');
        
        //chuyen chuoi ve dang ma hoa
        $res = str_replace("-","%",$res);
        
        
        /* list page */
        if($list_page != null){
            $list_page = explode("_",$list_page);
        }
        
        /*page current */
        $page = $list_page[$index];
        
        /* get list question and list answer */
        $list_question = $this->model->getListQuestion($serq_code,$page);
        $list_ansq = $this->model->getListAnsq($serq_code);
        
        /* get list result */
        $list = $this->model->getListResult($sess_code);
        $list_result = array();
        $list_resqcode = array();
        $list_require = array();
        $arr_quesedit = array();
        
        for($i = 0; $i < count($list); $i++){
            if($list[$i]->RESQ_TYPE == 1){
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE."_".$list[$i]->RESQ_VALUE] = $list[$i]->RESQ_CODE;
            }else{                
                $list_result[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_VALUE;
                $list_resqcode[$list[$i]->RESQ_SESS_CODE."_".$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_CODE;
            }
            //tao mang cau hoi duoc tra loi de kiem tra ren buoc
            $list_require[$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_QUES_CODE;
            
            $arr_quesedit[$list[$i]->RESQ_QUES_CODE] = $list[$i]->RESQ_QUES_CODE;
        }
       
        /* list answer of user */
        if($res != ''){
            $result = explode(".",$res);
        }
                
        /* save answer of user */
        $data = array();
        $data_edit = array();
        $data_del = array();
                
        $key = HelperBG::getIDAuto('resq','RESQ_CODE',20);
        $key = $key*1;
        if($result != null){
            foreach($result as $value){
                $arr = array();
                
                $temp = explode("_",$value);
                if(($arr_quesedit != null) && ($arr_quesedit[$temp[0]] == $temp[0])){
                    if($temp[2] == 1){
                        //DATA DELETE
                        $arr_del = array();
                        $arr_del['SESS_CODE'] = $sess_code;
                        $arr_del['QUES_CODE'] = $temp[0];
                        $data_del[] = $arr_del;                        
                        
                        // DATA INSERT
                        $arr['RESQ_CODE'] = $key;
                        $key++;
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        $arr['RESQ_VALUE'] = $temp[1];
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data[] = $arr;
                    }else{
                        $arr['RESQ_CODE'] = $list_resqcode[$sess_code."_".$temp[0]];
                        
                        $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                        $arr['RESQ_QUES_CODE'] = $temp[0];
                        
                        //giai ma gia tri da ma hoa
                        $str = urldecode($temp[1]);
                        $arr['RESQ_VALUE'] = $str;
                        $arr['RESQ_TYPE'] = $temp[2];
                        
                        $data_edit[] = $arr;                      
                    }
                                   
                    
                }else{
                    $arr['RESQ_CODE'] = $key;
                    $key++;
                    $arr['RESQ_SESS_CODE'] = $sess_code;
                    
                    $arr['RESQ_QUES_CODE'] = $temp[0];
                    
                    //giai ma gia tri da ma hoa
                    $str = urldecode($temp[1]);
                    $arr['RESQ_VALUE'] = $str;
                        
                    $arr['RESQ_TYPE'] = $temp[2];
                    
                    $data[] = $arr;
                }
                
            }
        }
        
        //echo "<pre>";print_r($data);echo "</pre>";
        //echo "<br/>";
        //echo "<pre>";print_r($data_edit);echo "</pre>";
        
        if($data_del != null){
            $this->model->removeResqFrSessQuess($data_del);
        }
        if($data != null){
            $this->model->saveResqInsert($data);
        }
        if($data_edit != null){
            $this->model->saveResqUpdate($data_edit);
        }
               
                
        /* show list question */
        if($list_question != null){
        
        echo '<div class="intro_page">'.$list_question[$index]->PAGQ_TITLE.'</div>
              <div class="content_intro">'.$list_question[$index]->PAGQ_DESCRIPTION.'</div>';
              
            for($i = 0; $i < count($list_question); $i++){
                $value = $list_question[$i];
        echo '<table style="width: 100%;">
                <tbody>
                <tr>
                    <td>
                    <div class="question">
                    
                        <div class="ques">
                            <label>'. ($i+1) .'.'. $value->QUES_TITLE .'</label>';
        if($value->QUES_REQUIRED == 1){ echo '<label class="required">※</label>'; }
        if(!empty($value->QUES_DESCRIPTION)){ echo '<br /><label>('.$value->QUES_DESCRIPTION.')</label>'; }
        echo            '</div>
                        <div class="answ">';
        
        
        /* hien cau tra loi cua cau hoi */               
        if($value->QUES_TYPE == 1){
            if($list_ansq != null){
                
                if($value->QUES_REQUIRED == 1){
                    if($list_require[$value->QUES_CODE] == $value->QUES_CODE){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="1" />';
                    }else{
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';
                    }
                      
                }
                                
                for($j = 0; $j < count($list_ansq); $j++){
                    if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                        $val = $list_ansq[$j];
                        echo '<input type="checkbox" class="checkbox_ans"';
                        
                        if($value->QUES_REQUIRED == 1){
                            echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                        }
                        
                        if($list_result[$sess_code."_".$value->QUES_CODE."_".$list_ansq[$j]->ANSQ_CODE] == $list_ansq[$j]->ANSQ_CODE){
                            echo 'checked="checked" name="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" id="cb'.$value->QUES_CODE."_".$val->ANSQ_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_1".'" /><label for="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                            //echo '<input type="hidden" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" class="input_edit" value="'.$value->QUES_CODE.'" />';
                        }else{
                            echo ' name="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" id="cb'.$value->QUES_CODE."_".$val->ANSQ_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_1".'" /><label for="cb'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                        
                    }
                }
            }                                
        }
                        
                        
        if($value->QUES_TYPE == 2){
            if($list_ansq != null){
                
                if($value->QUES_REQUIRED == 1){
                    if($list_require[$value->QUES_CODE] == $value->QUES_CODE){
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="1" />';
                    }else{
                        echo '<input type="hidden" class="input_check" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" value="" />';
                    }
                      
                }
                
                for($j = 0; $j < count($list_ansq); $j++){
                    if($value->QUES_CODE == $list_ansq[$j]->ANSQ_QUES_CODE){
                        $val = $list_ansq[$j];
                        echo '<input type="radio" class="radio_ans"';
                        
                        if($value->QUES_REQUIRED == 1){
                            echo ' onclick="document.getElementById('."'".'input'.$value->QUES_CODE."'".').value='."'".'1'."'".';" ';
                        }
                        
                        if($list_result[$sess_code."_".$value->QUES_CODE] == $list_ansq[$j]->ANSQ_CODE){
                            echo 'checked="checked"  id="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" name="rd'.$value->QUES_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_2".'"/><label for="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                            //echo '<input type="hidden" name="input'.$value->QUES_CODE.'" id="input'.$value->QUES_CODE.'" class="input_edit" value="'.$value->QUES_CODE.'" />';
                        }else{
                            echo ' id="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'" name="rd'.$value->QUES_CODE.'" value="'.$value->QUES_CODE.'_'.$val->ANSQ_CODE."_2".'"/><label for="rd'.$value->QUES_CODE.'_'.$val->ANSQ_CODE.'">'.$val->ANSQ_VALUE.'</label><br />';
                        }
                        
                    }
                }
            }                                
        }
        
        
                        
                        
        if($value->QUES_TYPE == 3){
            echo '<textarea name="'.$value->QUES_CODE.'" id="'.$value->QUES_CODE.'" class="text_ans"';
                if($value->QUES_REQUIRED == 1){
                    echo ' check = "1" ';
                }else{
                    echo ' check = "0" ';
                }
            echo ' cols="8" rows="3" style="width:300px" >';
            echo $list_result[$sess_code."_".$value->QUES_CODE];
            echo '</textarea>';
                                                       
        }
                        
                            
        echo           '</div>
                    </div>
                </td>
            </tr>
            </tbody>
            </table>';
        
            }
        
        echo '<table style="width: 100%;">
                <tfoot>
        			<tr>
        				<td >
        					<div class="container" style="margin-left:300px">
                                <div class="prev_page">';
                                if($index == 0){
       echo                         '<div class="button2-right off"><div class="prev"><span>前へ</span></div></div>';
                                }else{
       echo                         '<div class="button2-right">
                                        <div class="prev"><a href="#" title="Prev" class="link_page" id="link_page_prev" onclick="showQuesPrev('.$serq_code.",".($index-1).",".$sess_code.');" >前へ</a></div>
                                    </div>';
                                }
        
       echo                     '</div>
                                <div class="next_page">';
                                if($index == (count($list_page)-1)){
       echo                         '<div class="button2-left"><div class="end"><a href="#" title="End" class="link_end" id="link_end" onclick="showQuesEnd('.$serq_code.",".$sess_code.');" >終わり</a></div></div>';
                                }else{
       echo                         '<div class="button2-left">
                                        <div class="next"><a href="#" title="Next" class="link_page" id="link_page_next" onclick="showQuesNext('.$serq_code.",".($index+1).",".$sess_code.');" >次へ</a></div>
                                    </div>';
                                }   
       echo                     '</div>
                            </div>
                            </div>				
                        </td>
        			</tr>
        		</tfoot>
            </table>';
            
        }        
        
        $app = &JFactory::getApplication();
        $app->close();
        exit();
        
    }

}